<template>
	<div class="system-common-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="ruleFormRef" :model="state.ruleForm" :rules="state.rules" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="手机号码" prop="phone">
							<el-input v-model="state.ruleForm.phone" placeholder="请输入手机号码" clearable></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="批量上传">
							<el-upload v-model:file-list="fileList"
								action="https://localhost:5001/SysApi/Common/BlackList/upload" multiple :limit="1"
								:http-request="uploadFile" class="avatar-uploader">
                                <el-button size="default" type="warning" plain>
                                    <el-icon>
                                        <ele-UploadFilled/>
                                    </el-icon>
                                    导入文件
                                </el-button>
								<template #tip>
									<div class="el-upload__tip">
                                        文本文件大小少于300KB.
									</div>
								</template>
							</el-upload>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitText }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="editBlackList">
import { reactive, ref } from 'vue';
import apiBlackList from '/@/api/common/blackList/index';
import type { UploadProps, UploadUserFile } from 'element-plus'
import request from '/@/utils/request';
import { ElMessageBox, ElMessage } from 'element-plus';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
// 定义变量内容
const ruleFormRef = ref();
const state = reactive({
	ruleForm: {} as RowBlackListType,
	rules: {
		phone: [{ required: true, message: '请输入手机号码', trigger: 'blur' }],
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitText: '',
	},
});
const fileList = ref<UploadUserFile[]>([]);

// 打开弹窗
const openDialog = (type: string, row: RowBlackListType) => {
	fileList.value = [];
	if (type === 'edit') {
		state.ruleForm = row;
		state.dialog.title = '修改';
		state.dialog.submitText = '修 改';
	} else {
		state.dialog.title = '新增';
		state.dialog.submitText = '新 增';
		// ruleFormRef.value.resetFields
		// 清空表单，此项需加表单验证才能使用
	}
	state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
	emit('refresh');
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	ruleFormRef.value.validate(async (valid: boolean) => {
		if (!valid) return;
		let res;
		if (state.ruleForm.id != undefined && state.ruleForm.id != '') {
			res = await apiBlackList().editPost(state.ruleForm);
		} else {
			res = await apiBlackList().addPost(state.ruleForm);
		}
		if (res.data.resCode != '0000')
			ElMessage.error(res.data.resMessage);
		else {
			ElMessage.success("操作成功");
			closeDialog();
		}
	});
};

/**
 * 上传
 * @param {*} req
 */
const uploadFile = (req: any) => {
	const config = {
		headers: { 'Content-Type': 'multipart/form-data' }
	};
	const formdata = new FormData();
	formdata.append('file', req.file);
	request.post('/SysApi/Common/BlackList/upload', formdata, config).then((res) => {
		if (res.data.resCode != '0000') {
			ElMessage.error(res.data.resMessage);
		} else
			ElMessage.success(res.data.resMessage);
	});
}

// 导出对象
defineExpose({
	openDialog,
});
</script>
<style lang="scss" scoped>
.avatar-uploader .avatar {
	width: 360px;
	height: 180px;
	display: block;
}

.avatar-uploader :deep(.el-upload) {
	border: 1px dashed var(--el-border-color);
	border-radius: 6px;
	cursor: pointer;
	position: relative;
	overflow: hidden;
	transition: var(--el-transition-duration-fast);
}

.avatar-uploader :deep(.el-upload:hover) {
	border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
	font-size: 28px;
	color: #8c939d;
	width: 360px;
	height: 180px;
	text-align: center;
}
</style>
